home *** CD-ROM | disk | FTP | other *** search
- #include <Types.h>
- #include <QuickDraw.h>
- #include <OSUtils.h>
- #include <SegLoad.h>
- #include <Fonts.h>
- #include <Windows.h>
- #include <Menus.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Processes.h>
- #include <Files.h>
- #include <Devices.h>
- #include <Memory.h>
- #include <LowMem.h>
- #include <FSM.h>
- #include "LinkedList.h"
-
- // multiple of 512 bytes.
- #define amountToRead (1024L*1024L)
-
- #define kSleepTime 180
-
- #define kErrorAlertID 128
- #define kStatusAlertID 129
- #define kAboutAlertID 150
-
- #define kMenuBarID 128
- #define kAppleMenuID 128
- #define kFileMenuID 129
- #define kEditMenuID 130
- #define kDriverMenuID 131
-
- typedef struct DriverInfo {
- Str255 driverName;
- short driverRefNum;
- } DriverInfo, *DriverInfoPtr;
-
- Boolean gQuitting = false;
- myListPtr gTheList;
-
- static OSErr ToolBoxInit (void)
- {
- MaxApplZone();
- InitGraf (&qd.thePort);
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ((long)nil);
- InitCursor ();
-
- return noErr;
- }
-
- static short TestDriverForAsyncCapability (short refNum, short driveNum) {
- OSErr err;
- ParamBlockRec pBlock;
- Ptr buf = nil;
- int count = 0;
- Str255 errNumStr;
- Boolean isAsync = -1; // use -1 to mean there was an error
- long size = amountToRead;
-
- buf = NewPtr (size);
- err = MemError ();
- if (buf == nil) {
- // Couldn't get memory for read buffer
- NumToString (MemError(), errNumStr);
- ParamText ("\pCouldn't get memory for buffer, ", errNumStr, nil, nil);
- (void)StopAlert (kErrorAlertID, nil);
- }
-
- if (buf != nil) {
- // Read from block 0 off the device.
- pBlock.ioParam.ioCompletion = nil; // I'll just poll ioResult instead.
- pBlock.ioParam.ioVRefNum = driveNum; // physical drive number
- pBlock.ioParam.ioRefNum = refNum;
- pBlock.ioParam.ioBuffer = buf;
- pBlock.ioParam.ioReqCount = size;
- pBlock.ioParam.ioPosMode = fsFromStart & noCacheMask;
- pBlock.ioParam.ioPosOffset = 0;
-
- err = PBReadAsync(&pBlock);
- }
-
- if (err < noErr) {
- // Couldn't queue the request type error
- NumToString (err, errNumStr);
- ParamText ("\pCouldn't queue the request, error: ", errNumStr, nil, nil);
- (void)StopAlert (kErrorAlertID, nil);
- } else {
- // Spin while read happens async
- while (pBlock.ioParam.ioResult > 0) {
- count++;
- }
- }
-
- // After read completes, was there an error (bad block, etc.)?
- // This code only runs if PBReadAsync returned noErr so we don't
- // report the same error twice.
- if (err == noErr && pBlock.ioParam.ioResult != noErr) {
- NumToString (pBlock.ioParam.ioResult, errNumStr);
- ParamText ("\pDriver returned an error of: ", errNumStr, nil, nil);
- (void)StopAlert (kErrorAlertID, nil);
- } else if (err == noErr && pBlock.ioParam.ioResult == noErr) {
- if (count != 0) {
- // If we got to spin in the above while loop, driver is async...
- isAsync = 1;
- } else {
- // ...else it isn't async
- isAsync = 0;
- }
- }
-
- // If we have allocated any memory, release it.
- if (buf != nil) {
- DisposePtr (buf);
- }
-
- return isAsync;
- }
-
- static myListPtr GetDriverList (void) {
- Ptr UTblBase;
- Ptr p = (char*)0x01d2;
- DCtlHandle driverEntry;
- DRVRHeaderPtr driverHeader;
- long i;
- short UTblEntries;
- myListPtr theList;
- DriverInfoPtr drvrInfo;
-
- //UTblEntries = LMGetUnitTableEntryCount ();
- //LMGetUnitTableEntryCount doesn't seem to have any glue (can't link), so hit low mem ourselves...
- UTblEntries = *(short*)p;
- UTblBase = LMGetUTableBase ();
-
- theList = NewList ();
-
- if (theList != nil) {
- for (i = 0; i < UTblEntries; i++) {
- driverEntry = (DCtlHandle)((Handle)UTblBase)[i];
- if (driverEntry != nil) {
- driverHeader = (DRVRHeaderPtr)(*driverEntry)->dCtlDriver;
- if ((*driverEntry)->dCtlFlags & dRAMBasedMask) {
- driverHeader = *(DRVRHeaderHandle)driverHeader;
- }
-
- // If the driver isn't already open, don't display it
- if (!((*driverEntry)->dCtlFlags & dOpenedMask)) {
- driverHeader = nil;
- }
-
- if (driverHeader != nil) {
- drvrInfo = (DriverInfoPtr)NewPtr (sizeof (DriverInfo));
- if (drvrInfo != nil) {
- BlockMoveData (&driverHeader->drvrName, drvrInfo->driverName, driverHeader->drvrName[0]+1);
- drvrInfo->driverRefNum = (*driverEntry)->dCtlRefNum;
- AppendToList (drvrInfo, theList);
- }
- }
- }
- }
- }
-
- return theList;
- }
-
- static short GetFirstDriveNum (short refNum) {
- QHdrPtr driveQueue;
- DrvQElPtr driveQElem;
-
- driveQueue = GetDrvQHdr ();
- driveQElem = (DrvQElPtr)driveQueue->qHead;
-
- while (driveQElem != nil && driveQElem->dQRefNum != refNum) {
- driveQElem = (DrvQElPtr)driveQElem->qLink;
- }
-
- if (driveQElem->dQRefNum == refNum) {
- return driveQElem->dQDrive;
- } else {
- //We didn't find a match for this refNum
- return 0;
- }
- }
-
- static void DoMenuChoice (long menuChoice) {
- DriverInfoPtr listElm;
- short result;
-
- if (menuChoice) {
- short menu = HiWord (menuChoice),
- item = LoWord (menuChoice);
- switch (menu) {
- case kAppleMenuID:
- if (item == 1) {
- (void)Alert (kAboutAlertID, nil);
- } else {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- appleMenu = GetMenuHandle (kAppleMenuID);
- if (appleMenu != nil) {
- GetMenuItemText (appleMenu, item, accName);
- accNumber = OpenDeskAcc (accName);
- }
- }
- break;
- case kFileMenuID:
- gQuitting = true;
- break;
- case kEditMenuID:
- //HandleEditChoice (item);
- break;
- case kDriverMenuID:
- listElm = (DriverInfoPtr)GetItemNumFromList (item, gTheList);
- result = TestDriverForAsyncCapability (listElm->driverRefNum, GetFirstDriveNum (listElm->driverRefNum));
- if (result == 1) { //Driver is async capable
- ParamText ("\pasynchronous", nil, nil, nil);
- (void)NoteAlert (kStatusAlertID, nil);
- } else if (result == 0) { //Driver is not async capable
- ParamText ("\psynchronous", nil, nil, nil);
- (void)NoteAlert (kStatusAlertID, nil);
- } else {
- //status unknown because of error
- }
- break;
- }
- HiliteMenu (0);
- }
- }
-
- void main (void) {
- MenuHandle theMenu;
- Handle menuBar;
- DriverInfoPtr listElm;
- long i;
-
- ToolBoxInit ();
-
- menuBar = GetNewMBar (kMenuBarID);
- if (menuBar == nil) { /* No memory for our menu, can't recover */
- SysBeep (1);
- gQuitting = true;
- } else {
- SetMenuBar (menuBar);
-
- theMenu = GetMenuHandle (kAppleMenuID);
- if (theMenu == nil) {
- SysBeep (1);
- gQuitting = true;
- } else { /* No memory for our menu, can't recover */
- AppendResMenu (theMenu, 'DRVR');
- DrawMenuBar ();
-
- gTheList = GetDriverList ();
-
- // Make the Driver menu by inserting the name of each found driver
- i = 1;
- listElm = (DriverInfoPtr)GetItemNumFromList (i++, gTheList); // Gets a name from the list
- theMenu = GetMenuHandle (kDriverMenuID);
- while (listElm != nil) {
- AppendMenu (theMenu, listElm->driverName);
- listElm = (DriverInfoPtr)GetItemNumFromList (i++, gTheList);
- }
- }
- }
-
- while (gQuitting == false) {
- EventRecord event;
- Boolean gotEvent;
- WindowPtr window;
-
- gotEvent = WaitNextEvent (everyEvent, &event, kSleepTime, nil);
- if (gotEvent == true) {
- short thePart;
-
- switch (event.what) {
- case mouseDown:
- thePart = FindWindow (event.where, &window);
- if (thePart == inMenuBar) {
- DoMenuChoice (MenuSelect (event.where));
- } else if (thePart == inSysWindow) {
- SystemClick (&event, window);
- }
- break;
- case autoKey:
- case keyDown:
- if (event.modifiers & cmdKey)
- DoMenuChoice (MenuKey (event.message & charCodeMask));
- break;
- case activateEvt:
- case osEvt:
- case updateEvt:
- case kHighLevelEvent:
- case mouseUp:
- case keyUp:
- case diskEvt:
- break;
- }
- }
- }
- }
-
-